/**
 * Created by Administrator on 2016/8/23.
 */

var sqlClient = require('../lib/mysql/mysql');
var taskDao = module.exports;
var moment = require('moment');



/*
* 任务列表
*
* */
taskDao.getTaskList = function(taskType,page,size,cb){
    var sql = '';
    var sqlSelect = 'select * ';
    var sqlFrom = 'from rb_tasks ';
    //var sqlCondition = ' where state = 1 and task_type =' + taskType;
    var sqlCondition = ' where state = 1';
    var sqlSort = ' order by add_date desc limit ?,?'
    sql = sqlSelect + sqlFrom + sqlCondition + sqlSort;
    console.log(sql);
    sqlClient.query(sql,[parseInt((page - 1) * size), parseInt(size)], function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data);
    })
}


/*
*
* 任务列表的数量
* */
taskDao.getTaskCount = function(taskType,cb){
    var sql = '';
    var sqlSelect = 'select count(task_id) as counts ';
    var sqlFrom = 'from rb_tasks ';
    //var sqlCondition = ' where state = 1 and task_type =' + taskType;
    var sqlCondition = ' where state = 1';
    sql = sqlSelect + sqlFrom + sqlCondition ;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data[0].counts);
    })
}


/*
 * 根据taskId获取任务信息
 *
 * */
taskDao.getTaskByTaskId = function(taskId,cb){
    var sql = '';
    var sqlSelect = 'select * ';
    var sqlFrom = 'from rb_tasks ';
    var sqlCondition = 'where task_id = ' + taskId;
    sql = sqlSelect + sqlFrom + sqlCondition;
    console.log(sql);
    sqlClient.query(sql, null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }

        return cb && cb(null, data);
    })
}


/*
*
* 获取总任务下的子任务
*
* */
taskDao.getChildTask = function(taskId,cb){
    var sql = '';
    var sqlSelect = 'select * ';
    var sqlFrom = 'from rb_task_items ';
    var sqlCondition = ' where task_id = ' + taskId;

    sql = sqlSelect + sqlFrom + sqlCondition;
    console.log(sql);
    sqlClient.query(sql, null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data);
    })
}


/*
*
* 更改任务状态
*
* */
taskDao.updateTaskStatus = function(taskId,taskStatus,cb){
    var sql = '';
    var sqlUpdate = 'update rb_tasks ';
    var sqlSet = 'set status = ' + taskStatus;
    var sqlCondition = ' where task_id = ' + taskId;

    sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    sqlClient.query(sql, null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data);
    })
}

/*
 *
 * 更改任务的state
 *
 * */
taskDao.changeTaskState = function(taskId,cb){
    var sql = '';
    var sqlUpdate = 'update rb_tasks ';
    var sqlSet = 'set state = 0 ';
    var sqlCondition = 'where task_id = ' + taskId;

    sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    sqlClient.query(sql, null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data);
    })
}

/*
*
* 插入总任务
*
*
* */
taskDao.insertTask = function(taskName,icon,taskMoney,taskQuantity,taskItemsNumber,taskType,cb){
    var sql = '';
    var fields = [
        'task_name',
        'task_icon',
        'task_money',
        'task_total_quantity',
        'task_left_quantity',
        'task_items_number',
        'task_type',
        'state',
        'status'
    ];

    var values = [
        taskName,
        icon,
        taskMoney,
        taskQuantity,
        taskQuantity,
        taskItemsNumber,
        taskType,
        1,
        2
    ]
    var sql = 'insert into rb_tasks (' + fields.join(',') + ') values ("' + values.join('","') + '")';
    console.log(sql);
    sqlClient.query(sql, null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })
}




/*
*
* 插入子任务
* packetName:当子任务是分享时是分享标题
*
* */
taskDao.insertTaskItem = function(taskId,taskItemName,taskItemType,taskItemMoney,describe,taskItemLink,openTimeInterval,ranking,packetName,shareTitle,cb){
    var sql = '';
    var fields = [
        'task_id',
        'task_item_name',
        'task_item_type',
        'task_item_money',
        'task_item_describe',
        'task_item_link',
        'open_time_interval',
        'ranking',
        'packet_name',
        'share_title'

    ];

    var values = [
        taskId,
        taskItemName,
        taskItemType,
        taskItemMoney,
        describe,
        taskItemLink,
        openTimeInterval,
        ranking,
        packetName,
        shareTitle

    ]


    var sql = 'insert into rb_task_items (' + fields.join(',') + ') values ("' + values.join('","') + '")';
    console.log(sql);
    sqlClient.query(sql, null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })
}


//修改总任务
taskDao.updateTask = function(taskId,taskName,taskIcon,taskTotalQuantity,taskLeftQuantity,cb){
    var sql = '';
    var sqlUpdate = 'update rb_tasks ';
    var sqlSet = 'set task_name = "' + taskName + '",task_icon = "' + taskIcon + '",task_total_quantity = ' + taskTotalQuantity + ',task_left_quantity = ' + taskLeftQuantity;
    var sqlCondition = ' where task_id = ' + taskId;
    var sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    sqlClient.query(sql, null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }

        return cb && cb(null, data);
    })
}

//获取红包任务列表
taskDao.getRPTaskList = function(cb){
    var fields = [
      'task_id',
      'task_name'
    ];
    var sql = '';
    var sqlSelect = 'select ' + fields.join(',');
    var sqlFrom = ' from rb_tasks';
    var sqlCondition = ' where task_type = 2 and state = 1 and status = 2';
    var sql = sqlSelect +sqlFrom + sqlCondition;
    console.log(sql);
    sqlClient.query(sql, null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }

        return cb && cb(null, data);
    })



}